<?php
/**
 * 活动小程序相关服务类
 * Actutils.php BY My
 * Created by sen at 2021/3/23
 * AgeClub.net @ 2021
 */

namespace app\api\controller\bzh\business;


use app\admin\model\moyi\log\Like;
use app\api\library\Utils;
use app\common\controller\Api;
use think\Db;
use think\Log;
use think\Validate;
use think\Cache;
class Business extends Api
{
    protected $noNeedLogin = ['businessList','businessHot','productDetail','businessType','searchDetail','taxonomy','attention'];
    protected $noNeedRight = '*';


    /**
     * 获取展会产品列表
     */
    public function businessList()
    {

        if ($this->request->isGet()) {
            $type = $this->request->request('type', 1);
            $page = $this->request->request('page', 1);
            $total = Db::name('bzh_business')
                ->where('taxonomy', $type)
                ->where('status', 'normal')
                ->count();

            $limit = ($page-1) * 10;
            $list = Db::name('bzh_business')
                ->where('taxonomy', $type)
                ->where('status', 'normal')
                ->limit($limit, 10)
                ->field('id,company,log,hit,forward,collage')
                ->select();

            if(empty($list)){
                $this->error('暂无分类该更多展商',406);
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);
            $this->success('获取信息成功', $result, 200);
        }else{
            $this->error('请求方式有误',305);
        }

    }


    /**
     * 获取展会热门产品列表
     */
    public function businessHot()
    {
        if ($this->request->isGet()) {
            $list = Db::name('bzh_business')
                ->where('status', 'normal')
                ->where('switch', 1)
                ->limit(0, 10)
                ->field('id,company,log,hit,forward,collage')
                ->select();

            $result = array("rows" => $list);
            if (!empty($list)) {
                $this->success('获取信息成功', $result, 200);
            } else {
                $this->error('暂未设置热门资讯', 406);
            }
        }

    }


    /**
     * 获取展商详情信息
     */
    public function productDetail()
    {
//        var_dump($_SERVER['HTTP_TOKEN']);
        if ($this->request->isGet()) {
            $id = $this->request->request('id', '');
            $user_id = $this->request->request('user_id', '');
            Db::name('bzh_business')->where('id', $id)->setInc('hit');//点击加一
            $total = Db::name('bzh_business')
                ->where('id', $id)
                ->count();

            if ($total == '1') {
                $list = Db::name('bzh_business')
                    ->where('id', $id)
                    ->select();
            } else {
                $this->error('暂无该商户信息', '', 3010);
            }

                $guanzhu = Db::name('bzh_attention')
                    ->where('user_id',$user_id)
                    ->where('lei',1)
                    ->where('gx_id',$id)
                    ->where('status','normal')
                    ->count();


                if($guanzhu =='1'){
                    $state='1';//已关注
//
                }else{
                    $state='0';//未关注
//
                }
            $list = str_replace("img",'img width="100%"',$list);
            $list = collection($list)->toArray();
            $result = array("rows" => $list,"state"=>$state);
            $this->success('获取商户信息成功', $result, 200);
        }
    }

    /**
     * 获取展商详情信息
     */
//    public function productDetail()
//    {
//        $code = $_REQUEST['code'];
//        $secret = "你的appsecret";
//        $appid = "你的appid";
//        $url = "https://api.weixin.qq.com/sns/jscode2session";
//
//        $ch = curl_init();
//        curl_setopt($ch,CURLOPT_URL,$url);
//        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
//        curl_setopt($ch,CURLOPT_TIMEOUT,30);
//
//        $content = curl_exec($ch);
//        $status = (int)curl_getinfo($ch,CURLINFO_HTTP_CODE);
//        if ($status == 404) {
//            return $status;
//        }
//        curl_close($ch);
//        print_r($content);
//    }

    /**
     * 获取展会搜索结果
     */
    public function searchDetail()
    {
        if ($this->request->isGet()) {
            $company = $this->request->request('company', '');
            if(!$company){
                $this->error('请输入搜索条件', '', 3010);
            }

            $total = Db::name('bzh_business')
                ->where('company', 'like', "%$company%")
                ->count();

            if ($total == '0') {
                $this->error('暂无搜索结果', '', 3010);
            } else {
                $list = Db::name('bzh_business')
                    ->where('company', 'like', "%$company%")
                    ->field('id,company,log,hit,forward,collage')
                    ->select();
            }


            $list = collection($list)->toArray();
            $result = array("rows" => $list);
            $this->success('索搜成功', $result, 200);

        }
    }
    /**
     * 获取展会搜索结果
     */
    public function taxonomy()
    {
        if ($this->request->isGet()) {
            $list = Db::name('bzh_taxonomybus')
                ->field('id,taxonomy')
                ->select();

            $list = collection($list)->toArray();
            $result = array("rows" => $list);
            $this->success('获取成功', $result, 200);

        }
    }


//    展商关注方法
    public function attention()
    {
        if ($this->request->isPost()) {
            $open_id=$this->request->post('open_id');
            $businessid=$this->request->post('businessid');
            $res= Db::name('bzh_business')->where('id', $businessid)->setInc('collage');

            if ( !empty($open_id) and !empty($businessid)){
                $total = Db::name('bzh_attention')
                    ->where('open_id',$open_id)
                    ->count();
                $data=[
                    'open_id'=>$open_id,
                    'busattention'=>$businessid,
                ];

                if($total =='0'){
                    // 判断关注表中是否有数据没有插入有更新
                    $list = Db::name('bzh_attention')
                        ->insert($data);
                }else{//追加关注数据分割符号
                    $isatten = '';
                    $businessids =$businessid;
                    $businessid =  $businessid.',';
                    $list = Db::name('bzh_attention')->where('open_id',$open_id)->field('busattention')->find();
                    $data =$list['busattention'].$businessid;
                    if(!empty($list['busattention'])){
                        $arr = explode(',',$list['busattention']);//如果没有这个就加有的话就不用返回一个数据
                        foreach ($arr as $key => $value) {
                            if($value =="$businessids"){
                               $isatten= 'isatten';
                              $this->error('已经关注过了','405');

                            }
                        }

                        if(empty($isatten)){//如果是空的话就更新插入这个关注
                            DB::name('bzh_attention')
                                ->where('open_id',$open_id)
                                ->setField('busattention', "$data");
                            $this->success('关注成功','200');

//                            这里在更改一个商户college的状态加一

                        }

                    }

                }


            }


        }
    }








}
